package com.wyz.util;

import lombok.Data;

import java.io.Serializable;

/**
 * @author 无涯子
 * @version 1.0
 * @date 2023/5/14 15:09:11
 * @description 后端返回前端的统一格式，JSON类型
 */
@Data
public class ResponseResult<T> implements Serializable {
    /***
     * 是否返回成功
     */
    private Boolean success;
    /***
     * 错误状态码
     */
    private Integer code;
    /***
     * 错误描述
     */
    private String message;
    /***
     * 返回数据
     */
    private T data;
    /***
     * 时间戳
     */
    private Long timestamp;

    public ResponseResult(){
        this.timestamp = System.currentTimeMillis();
    }

    /**
     * @description 操作成功
     * @param code
     * @param message
     * @param data
     * @return com.wyz.util.ResponseResult<T>
     * @author 无涯子
     * @date 2023/5/14 15:34:39
     */
    public static <T> ResponseResult<T> success(int code,String message,T data){
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setSuccess(true);
        responseResult.setCode(code);
        responseResult.setMessage(message);
        responseResult.setData(data);
        return responseResult;
    }

    public static <T> ResponseResult<T> success(int code,String message){
        return success(code,message);
    }

    public static <T> ResponseResult<T> success(String message,T data){
        return success(ResultCode.SUCCESS.getCode(),message,data);
    }

    public static <T> ResponseResult<T> success(String message){
        return success(message,null);
    }

    public static <T> ResponseResult<T> success(T data){
        return success(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
    }

    public static <T> ResponseResult<T> success(){
        return success(null);
    }


    /**
     * @description 操作失败
     * @param code
     * @param message
     * @param data
     * @return com.wyz.util.ResponseResult<T>
     * @author 无涯子
     * @date 2023/5/14 15:43:04
     */
    public static <T> ResponseResult<T> fail(int code,String message,T data){
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setSuccess(false);
        responseResult.setCode(code);
        responseResult.setMessage(message);
        responseResult.setData(data);
        return responseResult;
    }

    public static <T> ResponseResult<T> fail(int code,String message){
        return fail(code,message,null);
    }

    public static <T> ResponseResult<T> fail(String message,T data){
        return fail(ResultCode.FAIL.getCode(), message,data);
    }

    public static <T> ResponseResult<T> fail(String message){
        return fail(message,null);
    }

    public static <T> ResponseResult<T> fail(){
        return fail(ResultCode.FAIL.getMessage());
    }

    public static <T> ResponseResult<T> decide(boolean b){
        return decide(b,ResultCode.SUCCESS.getMessage(),ResultCode.FAIL.getMessage());
    }
    public static <T> ResponseResult<T> decide(boolean b,String success,String fail){
        if(b){
            return success(success);
        }else{
            return fail(fail);
        }
    }



}
